/*
 * L9Animation.cpp
 *
 *  Created on: 2011-10-18
 *      Author: baixp
 */

#include "../inc/L9Animation.h"
#include "L9Sprite.h"
namespace lib9bada {
L9Animation::L9Animation(L9Sprite* const sprite, int animIndex) {
	_sprite = sprite;
	curAnim = animIndex;
	setAnimPlayInfo(0, 0, true);
}
L9Animation::L9Animation(const L9Animation& anim){
	_sprite=anim._sprite;
	curAnim=anim.curAnim;
	setAnimPlayInfo(0, 0, true);
}
L9Animation& L9Animation::operator=(const L9Animation& anim){
	if(this!=&anim){//排除自赋值的情况
		_sprite=anim._sprite;
		curAnim=anim.curAnim;
		setAnimPlayInfo(0, 0, true);
	}
	return *this;
}
L9Animation::~L9Animation() {
	// TODO Auto-generated destructor stub
}

int L9Animation::getAFrameID(int aframe) {
	return _sprite->getAFrameID(curAnim, aframe);
}

int L9Animation::getAFrameX(int aframe) {
	return _sprite->getAFrameX(curAnim, aframe);
}

int L9Animation::getAFrameY(int aframe) {
	return _sprite->getAFrameY(curAnim, aframe);
}

int L9Animation::getAFrameTime(int aframe) {
	return _sprite->getAFrameTime(curAnim, aframe);
}

int L9Animation::getAnimWidth() {
	return _sprite->getAnimWidth(curAnim);
}
int L9Animation::getAnimHeight() {
	return _sprite->getAnimHeight(curAnim);
}

int L9Animation::getAnimFPS() {
	return _sprite->getAnimFPS(curAnim);
}

int L9Animation::getAFrames() {
	return _sprite->getAFrames(curAnim);
}
int L9Animation::getACurFrame() {
	return _curFrame;
}

int L9Animation::getACurTime() {
	return _curTime;
}

int L9Animation::getAnimX() {
	return _anim_PosX;
}

int L9Animation::getAnimY() {
	return _anim_PosY;
}

bool L9Animation::isLoopPlay() {
	return _bLoopPlay;
}
void L9Animation::setAnim(int anim_PosX, int anim_PosY, bool bLoopPlay) {
	_anim_PosX = (short) anim_PosX;
	_anim_PosY = (short) anim_PosY;
	_bLoopPlay = bLoopPlay;
}

void L9Animation::setAnimPlayInfo(int curFrame, int curTime, bool bPlayOver) {
	_curFrame = (short) curFrame;
	_curTime = (short) curTime;
	_bPlayOver = bPlayOver;
}
void L9Animation::playAnim(int anim_PosX, int anim_PosY) {
	setAnim(anim_PosX, anim_PosY, false);
	setAnimPlayInfo(0, 0, false);
}
bool L9Animation::isPlayOver() {
	if (_bLoopPlay) {
		return false;
	}
	return _bPlayOver;
}

void L9Animation::stopPlay() {
	_bLoopPlay = false;
	_bPlayOver = true;
}

void L9Animation::setSkipFrame(vector<int> skipFrameIndex) {
	AnimationSkipFrameIndex = skipFrameIndex;
}

void L9Animation::updateAnimation(Canvas* g) {
	if (isPlayOver()) { //动画已经播放结束
		return;
	}
	_curTime++;

	int frame_index = getAFrameID(_curFrame);
	int time = getAFrameTime(_curFrame);

	if (-1 != L9Util::getIndexByID(AnimationSkipFrameIndex, _curFrame)) { //播放动画时跳过的帧,这个主要的原因是 该帧用去做缩略图了,注意这里是动画中帧的 索引，不是帧ID
		_curFrame++;
		_curTime = 0;
		return;
	}
	//允许坐标为负数
	int af_x = getAFrameX(_curFrame);
	int af_y = getAFrameY(_curFrame);
	_sprite->paintFrame(g, frame_index, af_x + _anim_PosX, af_y + _anim_PosY);

	if (_curTime >= time) {
		_curFrame++;
		_curTime = 0;
		if (_curFrame >= getAFrames()) {
			_curFrame = 0;
			_curTime = 0;
			_bPlayOver = true;
		}
	}
}
};
